package com.example.demo.service;

import com.example.demo.entity.Customer;
import com.example.demo.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CustomerService {
    @Autowired
    CustomerRepository customerRepository;


    public Customer getCustomer(Integer id){
        Customer customer = customerRepository.findOne(id);
        return customer;
    }

    public Customer insertCustomer(HttpServletRequest request,Map customer){
        Customer cusTor = checkCustomer(customer,request);
        Customer save = customerRepository.save(cusTor);
        return save;
    }
    public Page<Customer> findCustomerCriteria(Integer page, Integer size, final Map customerQuery,String userId) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");
        Page<Customer> customerPage = customerRepository.findAll(new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> lstPredicates = new ArrayList<Predicate>();
                if( !StringUtils.isEmpty(customerQuery.get("id")) ){
                    lstPredicates.add(criteriaBuilder.like(root.get("id").as(String.class), "%"+customerQuery.get("id")+"%"));
                }
                if( !StringUtils.isEmpty(customerQuery.get("customerName")) ){
                    lstPredicates.add(criteriaBuilder.like(root.get("customerName").as(String.class), "%"+customerQuery.get("customerName")+"%"));
                }
                lstPredicates.add(criteriaBuilder.equal(root.get("updater").as(String.class), userId));
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
                return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
            }
        }, pageable);
        return customerPage;
    }

    public Boolean customerDelete(Integer id){
        customerRepository.delete(id);
        return true;
    }

    // 检查客户信息是否合法
    private  Customer checkCustomer(Map customer, HttpServletRequest request){
        Customer newCustomer = new Customer();
        if( !StringUtils.isEmpty(customer.get("id").toString()) ){
            newCustomer.setId( Integer.valueOf(customer.get("id").toString()) );
        }
        if( !StringUtils.isEmpty(customer.get("customerName").toString()) ){
            System.out.println(customer.get("customerName"));
            System.out.println(customer.get("customerName").toString());
            newCustomer.setCustomerName(customer.get("customerName").toString() );
            System.out.println(newCustomer.getCustomerName());
        }
        if( customer.get("customerSex") != null ){
            newCustomer.setCustomerSex( Integer.valueOf(customer.get("customerSex").toString()) );
        }
        if( !StringUtils.isEmpty(customer.get("customerAddress").toString()) ){
            newCustomer.setCustomerAddress(customer.get("customerAddress").toString() );
        }
        if( customer.get("customerPhone") != null ){
            newCustomer.setCustomerPhone(Long.valueOf(customer.get("customerPhone").toString()));
        }
        if( !StringUtils.isEmpty(customer.get("customerEmail").toString()) ){
            newCustomer.setCustomerEmail(customer.get("customerEmail").toString() );
        }
        if( !StringUtils.isEmpty(customer.get("customerContent").toString()) ){
            newCustomer.setCustomerContent(customer.get("customerContent").toString() );
        }
        newCustomer.setUpdateTime(new Date());
        HttpSession session = request.getSession();
        String userId = session.getAttribute("user").toString();
        newCustomer.setUpdater(userId);
        return newCustomer;
    }

}
